package com.example.easytraffic.core;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ScheduledTraffic implements Traffic{

	private Date scheduledDate;
	private Traffic traffic;
	private Timer timer;
	
	public ScheduledTraffic(Traffic traffic, Date scheduledDate) {
		super();
		this.timer = new Timer();
		this.scheduledDate = scheduledDate;
		this.traffic = traffic;
	}

	public void run() {
		try {
			timer.schedule(new TimerTask() {
				public void run() {
					traffic.run();
				}
			}, scheduledDate);
		} catch (TrafficEndedException e) {
			stop();
		}
	}
	
	public void stop() {
		timer.cancel();
		traffic.stop();
	}
}
